package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/customer"
    customerReq "counter-help/admin/server/model/customer/request"
)

type CusCustomizationService struct {}

// CreateCusCustomization 创建cusCustomization表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusCustomizationService *CusCustomizationService) CreateCusCustomization(cusCustomization *customer.CusCustomization) (err error) {
	err = global.GVA_DB.Create(cusCustomization).Error
	return err
}

// DeleteCusCustomization 删除cusCustomization表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusCustomizationService *CusCustomizationService)DeleteCusCustomization(ID string) (err error) {
	err = global.GVA_DB.Delete(&customer.CusCustomization{},"id = ?",ID).Error
	return err
}

// DeleteCusCustomizationByIds 批量删除cusCustomization表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusCustomizationService *CusCustomizationService)DeleteCusCustomizationByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customer.CusCustomization{},"id in ?",IDs).Error
	return err
}

// UpdateCusCustomization 更新cusCustomization表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusCustomizationService *CusCustomizationService)UpdateCusCustomization(cusCustomization customer.CusCustomization) (err error) {
	err = global.GVA_DB.Model(&customer.CusCustomization{}).Where("id = ?",cusCustomization.ID).Updates(&cusCustomization).Error
	return err
}

// GetCusCustomization 根据ID获取cusCustomization表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusCustomizationService *CusCustomizationService)GetCusCustomization(ID string) (cusCustomization customer.CusCustomization, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&cusCustomization).Error
	return
}

// GetCusCustomizationInfoList 分页获取cusCustomization表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusCustomizationService *CusCustomizationService)GetCusCustomizationInfoList(info customerReq.CusCustomizationSearch) (list []customer.CusCustomization, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&customer.CusCustomization{})
    var cusCustomizations []customer.CusCustomization
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&cusCustomizations).Error
	return  cusCustomizations, total, err
}